package com.gykfbxxh.imovie.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.AppBaseAdapter;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.bean.film.FilmListEntity;
import com.gykfbxxh.imovie.bean.news.ViewFilmsBigImg;
import com.gykfbxxh.imovie.bean.news.ViewFilmsVideo;
import com.gykfbxxh.imovie.bean.news.ViewFilmsVideo_B;
import com.gykfbxxh.imovie.bean.news.ViewLeftImg;
import com.gykfbxxh.imovie.bean.news.ViewNotice;
import com.gykfbxxh.imovie.bean.news.ViewRightImg;
import com.gykfbxxh.imovie.bean.news.ViewScrollView;
import com.gykfbxxh.imovie.config.ConfigKey;
import com.gykfbxxh.imovie.greendao.GDManager;
import com.gykfbxxh.imovie.manager.AppConfigManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by xuxinghai
 * On 2017/8/30
 */

public class FilmsListAdapter extends AppBaseAdapter<FilmListEntity> {
    public static final int DEFAULT = 0;//没有匹配到类型
    public static final int FILMS_RIGHT_IMAGE = 1;// 文章一个图片
    public static final int VIDEO_BIG_IMAGE = 2;// 视频大图
    public static final int VIDEO_BIG_IMAGE_B = 3;// 视频大图有边
    public static final int BIG_IMAGE = 4;// 影评图片大图
    public static final int SCROLL_IMAGE = 5;// 左右滑动视图
    public static final int NOTICE_VIEW = 6;// 通知消息列表
    public static final int FILMS_LEFT_IMAGE = 7;//左图带勾选按钮
    private int typeFlag;//当前适配的栏目类型
    private boolean isCheckVisible;
    private boolean isAllCheck;//是否全部选中
    private static HashMap<Integer, FilmEntity> isSelected;
    List<String> likeData = null ;//用户收藏本地数据库列表
    boolean isbeginStyle ;

    public FilmsListAdapter(List<FilmListEntity> list, Context context, int typeFlag) {
        super(list, context);
        this.typeFlag = typeFlag;
        getLikeLookData();
        isbeginStyle = AppConfigManager.getInstance().getConfigSP().getBoolean(ConfigKey.beginsStyle, false);
    }

    public FilmsListAdapter(List<FilmListEntity> list, Context context, int typeFlag,boolean isCheckVisible , boolean isAllCheck) {
        super(list, context);
        this.typeFlag = typeFlag;
        this.isCheckVisible = isCheckVisible;
        this.isAllCheck = isAllCheck;
        isSelected = new HashMap<>();
        getLikeLookData();
        isbeginStyle = AppConfigManager.getInstance().getConfigSP().getBoolean(ConfigKey.beginsStyle, false);
    }

    private void getLikeLookData() {
        likeData = GDManager.getInstance().getLikeLookListData();
        if(likeData==null) {
            likeData = new ArrayList<>();
        }
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        FilmListEntity listBean = list.get(position);
        if (listBean == null) {
            listBean = new FilmListEntity();
        }
        if(listBean.getFObject()==null) {
            listBean.setFObject(new FilmEntity());
        }
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case SCROLL_IMAGE://左右滑动视图
                    convertView = inflater.inflate(R.layout.item_scroll_img,null);
                    ViewScrollView viewScrollView = new ViewScrollView(convertView,context);
                    viewScrollView.loadData(listBean,likeData);
                    break;
                case FILMS_RIGHT_IMAGE://一张右图
                    convertView = inflater.inflate(R.layout.item_filmslist_image, null);
                    ViewRightImg viewFilmsImg = new ViewRightImg(convertView);
                    viewFilmsImg.loadData(context,listBean);
                    break;
                case FILMS_LEFT_IMAGE://一张左图
                    convertView = inflater.inflate(R.layout.item_left_image, null);
                    ViewLeftImg viewFilmsImg1 = new ViewLeftImg(convertView,context);
                    viewFilmsImg1.loadData(context,listBean,isCheckVisible,isSelected,position,isAllCheck);
                    break;
                case BIG_IMAGE://一张大图
                    convertView = inflater.inflate(R.layout.item_filmslist_bigimg, null);
                    ViewFilmsBigImg viewFilmsBigImg = new ViewFilmsBigImg(convertView);
                    viewFilmsBigImg.loadData(context,listBean);
                    break;
                case VIDEO_BIG_IMAGE://视频大图
                    convertView = inflater.inflate(R.layout.item_filmslist_video, null);
                    ViewFilmsVideo viewFilmsVideo = new ViewFilmsVideo(convertView,context);
                    viewFilmsVideo.loadData(listBean,typeFlag);
                    break;
                case VIDEO_BIG_IMAGE_B://视频大图有边
                    convertView = inflater.inflate(R.layout.item_filmslist_video_b, null);
                    ViewFilmsVideo_B viewFilmsVideo_b = new ViewFilmsVideo_B(convertView,context);
                    viewFilmsVideo_b.loadData(listBean,typeFlag);
                    break;
                case NOTICE_VIEW://通知布局
                    convertView = inflater.inflate(R.layout.item_notice, null);
                    ViewNotice viewNotice = new ViewNotice(convertView);
                    viewNotice.loadData(context,listBean);
                    break;
                default:
                    convertView = inflater.inflate(R.layout.item_layout_gone, null);
                    break;
            }
        } else {
            switch (type) {
                case SCROLL_IMAGE:
                    ViewScrollView viewScrollView = (ViewScrollView) convertView.getTag();
                    viewScrollView.loadData(listBean,likeData);
                    break;
                case FILMS_RIGHT_IMAGE:
                    ViewRightImg viewFilmsImg = (ViewRightImg) convertView.getTag();
                    viewFilmsImg.loadData(context,listBean);
                    break;
                case FILMS_LEFT_IMAGE:
                    ViewLeftImg viewFilmsImg1 = (ViewLeftImg) convertView.getTag();
                    viewFilmsImg1.loadData(context,listBean,isCheckVisible,isSelected,position,isAllCheck);
                    break;
                case BIG_IMAGE://一张大图
                    ViewFilmsBigImg viewFilmsBigImg = (ViewFilmsBigImg) convertView.getTag();
                    viewFilmsBigImg.loadData(context,listBean);
                    break;
                case VIDEO_BIG_IMAGE:
                    ViewFilmsVideo viewFilmsVideo = (ViewFilmsVideo) convertView.getTag();
                    viewFilmsVideo.loadData(listBean,typeFlag);
                    break;
                case VIDEO_BIG_IMAGE_B:
                    ViewFilmsVideo_B viewFilmsVideo_b = (ViewFilmsVideo_B) convertView.getTag();
                    viewFilmsVideo_b.loadData(listBean,typeFlag);
                    break;
                case NOTICE_VIEW:
                    ViewNotice viewNotice = (ViewNotice) convertView.getTag();
                    viewNotice.loadData(context,listBean);
                    break;
                default:
                    break;
            }
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 8;
    }


    /**
     * 得到UI类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if(list.get(position)==null||list.get(position).getFObject()==null) {
            return DEFAULT;
        }
        switch (typeFlag){
            case 1:
                if(position==0) {
                    return SCROLL_IMAGE;
                }
                if(isbeginStyle) {
                    return VIDEO_BIG_IMAGE ;
                }else {
                    return FILMS_RIGHT_IMAGE;
                }
            case 2:
                if(isbeginStyle) {
                    return VIDEO_BIG_IMAGE ;
                }else {
                    return FILMS_RIGHT_IMAGE;
                }
            case 6:
                return NOTICE_VIEW;
            case 7:
                return FILMS_LEFT_IMAGE;
            default:
                return DEFAULT;
        }

//        FilmListBean.SearchHot.ListBean listBean = list.get(position);
//        if (listBean == null) {
//            return DEFAULT;
//        }
//        // 文章类型(1.一张小图 ,2 大图视频)
//        switch (listBean.getFtype()) {
//            //适配所有文章
//            case TypeConstant.PAGE_ONE_IMAGE:
//                return FILMS_RIGHT_IMAGE;
//            case TypeConstant.PAGE_VIDEO:
//                return VIDEO_BIG_IMAGE ;
//            default:
//                return DEFAULT;
//        }
    }

    public static HashMap<Integer, FilmEntity> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, FilmEntity> isSelected) {
        FilmsListAdapter.isSelected = isSelected;
    }
}
